package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"guliedu/middleware"
	"guliedu/model"
	"guliedu/utils"
	"net/http"
	"strings"
	"time"
)

type registerInfo struct {
	Username   string `json:"username"`
	Email      string `json:"email"`
	Code       string `json:"code"`
	Nickname   string `json:"nickname"`
	Password   string `json:"password"`
	RePassword string `json:"repassword"`
}

type loginInfo struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

func LoginMember(ctx *gin.Context) {
	login := &loginInfo{}
	ctx.ShouldBind(&login)

	// todo 根据用户名查询用户
	member, _ := ucenterMemberService.Select(login.Username)
	if member.ID == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    20000,
			"message": "登入失败，用户名或密码错误！",
		})
		return
	}

	// todo 比对数据库信息和传来信息
	err := utils.Decrypt(member.Password, login.Password)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    20000,
			"message": "登入失败，用户名或密码错误！",
		})
		return
	}

	// todo 生成token返回给前端
	token, _ := middleware.GetToken(login.Username)
	fmt.Println(token)
	ctx.JSON(http.StatusOK, gin.H{
		"code":  20000,
		"token": token,
	})
	return
}

func GetMemberInfo(ctx *gin.Context) {
	token1 := ctx.Request.Header.Get("Authorization")

	token := strings.Split(token1, " ")[1]
	userToken, _ := middleware.AuthToken(token)
	member, _ := ucenterMemberService.Select(userToken.UserName)

	ctx.JSON(http.StatusOK, gin.H{
		"code":   20000,
		"member": member,
	})
	return
}

func RegisterMember(ctx *gin.Context) {
	register := &registerInfo{}
	ctx.ShouldBind(&register)
	fmt.Println(register)

	// todo 后期还需要验证验证码是否正确
	code, _ := ucenterMemberService.GetMemberCode(register.Email)
	if code != register.Code {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 20000,
			"msg":  "验证码错误！",
		})
		return
	}

	// 两次密码是否一致
	if register.Password != register.RePassword {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 20000,
			"msg":  "两次密码不一致！",
		})
		return
	}

	// 检查用户名是否存在
	member, _ := ucenterMemberService.Select(register.Username)
	if member.Username != "" {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 20000,
			"msg":  "用户名已存在！",
		})
		return
	}

	// todo 对密码进行加密
	encrypPwd := utils.Encryption(register.Password)
	m := &model.UCenterMember{
		Username:   register.Username,
		Password:   encrypPwd,
		Nickname:   register.Nickname,
		Sex:        0,
		Age:        0,
		Avatar:     "http://qiniu.xiaolatiao.cn/u%3D3104168140%2C3257774526%26fm%3D26%26gp%3D0.jpg",
		Sign:       "这里是个性签名哦~",
		IsDisabled: 2,
	}
	ucenterMemberService.Insert(m)

	// 增加每日统计
	date := time.Now().Format("2006-01-02")
	daily, _ := statisticsDailyService.Select(date)
	if daily.ID != 0 {
		daily.RegisterNum++
		statisticsDailyService.Update(daily)
	} else {
		d := &model.StatisticsDaily{
			DateCalculated: date,
			RegisterNum:    1,
			LoginNum:       0,
			VideoNum:       0,
			CourseNum:      0,
		}
		statisticsDailyService.Insert(d)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
	})
	return
}

func MemberSendEmail(ctx *gin.Context) {
	email := ctx.Param("email")
	ucenterMemberService.MemberSendEmail(email)
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
	})
	return
}
